package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.QuickSelectProductTag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface QuickSelectProductTagRepository extends JpaRepository<QuickSelectProductTag, String>, JpaSpecificationExecutor {

//    @Query(value = "select a from QuickSelectProductTag a, QuickSelectProductTag b where a.wId = b.wId and a.category = :category and a.subCategory != 'subCategory' and b.subCategory != 'subCategory' and b.code in :codes and a.code not in :codes order by a.name")
//    List<QuickSelectProductTag> getRelevants(@Param("category") String category, @Param("codes") List<String> codes);

    @Query(value = "select a.w_id from quick_select_product_tag a where a.code in :codes group by a.w_id having count(1) >= :flavorCount", nativeQuery = true)
    List<String> getWIdsByFlavor(@Param("codes") List<String> codes, @Param("flavorCount") int flavorCount);

    @Query(value = "select a from QuickSelectProductTag a where a.category='flavor' and a.subCategory != 'subCategory' and a.wId in :wIds order by a.name")
    List<QuickSelectProductTag> getFlavorsByWIds(@Param("wIds") List<String> wIds);

//    @Query(value = "select a.* from quick_select_product_tag a where a.w_id in :wIds order by a.w_id", nativeQuery = true)
//    List<QuickSelectProductTag> getAllByWIds(@Param("wIds") List<String> wIds);

    List<QuickSelectProductTag> queryByCategoryAndSubCategory(@Param("category") String category, @Param("subCategory") String subCategory);

    @Query(value = "select a from QuickSelectProductTag a where a.code in :codes and (a.subCategory is null or a.subCategory != 'subCategory') ")
    List<QuickSelectProductTag> getTagsByCodes(@Param("codes") List<String> codes);

    List<QuickSelectProductTag> queryByWId(String wId);

}
